package com.ziyucoding.springboot.labs.redislike.utils;

import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import com.ziyucoding.springboot.labs.redislike.service.DBLikeService;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 定时任务工具类
 *
 * @author ziyucoding
 * @date 2023-02-07
 **/
@Slf4j
public class CronUtil extends QuartzJobBean {

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private DBLikeService dbLikeService;

    /**
     * 定时执行任务
     *
     * @param context
     * @return void
     */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        log.info("LikeTask-------- {}", sdf.format(new Date()));

        //将 Redis 里的点赞信息同步到数据库里
        dbLikeService.transLikedFromRedis2DB();
        dbLikeService.transLikedCountFromRedis2DB();
    }

}
